var player = {
    changeWidth: function(ele, toWidth){
        $(ele).parent().find('.sbtnCurrent').removeClass('sbtnCurrent').addClass('sbtn');
        $(ele).removeClass('sbtn').addClass('sbtnCurrent');
            
        //没有toWidth参数则为恢复默认大小
        $("#playerWrap embed").each(function(){
            var p = $(this);
            var w = Number(p.attr('width'));
            var h = Number(p.attr('height'));
            if(toWidth){
                var r = toWidth/w;
                w = toWidth;
                h = h*r;
            }
            if(w > 620){
                $(".player").css('width', '950px');
            }else{
                $(".player").css('width', '590px');
            }
            p.width(w).height(h);
        });
    }
};

function append_comments(comments, replace) {
	var tpl = '\
		<?js for (var i = 0, n = comments.length; i < n; i++) { ?>\n\
		<?js     var comment = comments[i]; ?>\n\
		<?js     var screen_name_link = get_screen_name_link(comment.user, false); ?>\n\
	    <?js     var reply = comment.reply_comment && comment.reply_comment.user ? "回复" + get_screen_name_link(comment.reply_comment.user, true) + ": " : ""; ?>\n\
		<?js     var text = reply + format_comment_text(comment.text); ?>\n\
		<?js     var user = comment.user; ?>\n\
		<li class="clearfix">\
			<div class="usericon">\
				<a target="_blank" href="${user.t_url}" title="${user.description}">\
					<img src="${user.profile_image_url}" />\
				</a>\
			</div>\
			<div class="c_content">\
				<div class="userName">#{screen_name_link}: <span class="dt">${comment.created_at}</span>&nbsp;&nbsp;<a class="reply_btn hide" comment_id="${comment.id}" href="javascript:">[回复]</a></div>\
                <div class="comment">#{text}</div>\
                <?js if(comment.reply_comment && comment.reply_comment.user){ ?>\n\
                <?js     var comment = comment.reply_comment; ?>\n\
                <?js     var screen_name_link = get_screen_name_link(comment.user, false); ?>\n\
                <?js     var reply = comment.reply_comment ? "回复" + get_screen_name_link(comment.reply_comment.user, true) + ": " : ""; ?>\n\
                <?js     var text = reply + format_comment_text(comment.text); ?>\n\
                <?js     var user = comment.user; ?>\n\
                <div class="reply">\
                    <div class="userName">#{screen_name_link} 的评论: </div>\
                    <div class="replyComment">#{text}</div>\
                </div>\
                <?js } ?>\n\
            </div>\
		</li>\
		<?js } ?>';
	var html = Shotenjin.render(tpl, {'comments': comments});
	if(replace) {
		$('#comments').html(html);
	} else {
		var old_html = $('#comments').html();
		if($('#comments').attr('empty') == 'true') {
			old_html = '';
		}
		$('#comments').html(html + old_html);
	}
	if(get_user_id() != null) { // 只有登录用户才显示回复
        $('a.reply_btn').click(function() {
            var $this = $(this);
            $('#reply_comment_id').val($this.attr('comment_id'));
            var screen_name = $this.parents('.c_content').find('.userName a').html();
            var text = $this.parents('.c_content').find('.comment').html();
            $('#reply_comment_to').html('回复@' + screen_name + ' 的信息: ' + text + ' <a class="cancel_reply_btn right" href="javascript:cancel_reply();">取消回复</a>').show();
            $('#comment_body').focus();
        }).show();
    }
}

function list_comments() {
	$('#comments_loading').show();
	var movie_id = $('#movie_id').val();
	var url = '/movie/' + movie_id + '/comment/list';
	$.getJSON(url, function(data) {
		$('#comments_loading').hide();
		if(data.success) {
			if(data.comments.length > 0) {
				append_comments(data.comments, true);
			} else {
				$('#comments').html('沙发还在，赶紧抢吧!').attr('empty', 'true');
			}
		} else {
			$('#comments').html('获取评论异常: ' + data.error);
		}
	});
}

// 取消回复
function cancel_reply() {
	$('#reply_comment_id').val('');
	$('#reply_comment_to').hide();
}

$(document).ready(function(){
    // 加载评论
	list_comments();
	
	// 添加评论
	$('#comment_btn').click(function() {
		add_request_disabled_control($(this));
		var comment = $('#comment_body').val();
		var movie_id = $('#movie_id').val();
		var cid = $('#reply_comment_id').val();
		var retweet = $('#retweet').attr('checked');
		if(retweet) {
			retweet = 1;
		} else {
			retweet = 0;
		}
		cancel_reply();
		var url = '/movie/' + movie_id + '/comment/add';
		$.getJSON(url, {comment: comment, cid: cid, retweet: retweet}, function(data) {
			if(data.success) {
				append_comments([data.comment]);
				$('#comment_body').val('');
			} else {
				alert(data.error);
			}
		});
	});
});